﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using OrderMgr.Rpt;
using OrderMgr.CustomerInfo;
using OrderMgr.OrderInfo;
using TierApplication;


namespace OrderMgr.Rpt
{
    public partial class frmRptSaleDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _OrderID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_Order mOrder = new T_Order();
        SqlDataAdapter sdaOrderDetail = new SqlDataAdapter();
        DataSet dsOrderDetail = new DataSet();

        DataTable dtOrderDetailLocal = new TdsOrder.TOrderDetailDataTable();

        #endregion

        public frmRptSaleDetail()
        {
            InitializeComponent();
            this.Text = "销售单明细查询";
        }

        private void frmRptSaleDetail_Load(object sender, EventArgs e)
        {
            TierService.Utility.SetFeeVisible(clUnitPrice, TierService.Utility.gSeeAllFeeData);
            TierService.Utility.SetFeeVisible(clSumPrice, TierService.Utility.gSeeAllFeeData);
            TierService.Utility.SetFeeVisible(clOrderActMoney, TierService.Utility.gSeeAllFeeData);
            TierService.Utility.SetFeeVisible(clOrderActMoneyM, TierService.Utility.gSeeAllFeeData);

            DataTable dt = TierService.Utility.InVisibleObject("iRptSaleAnalysis");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string  sControlID= dt.Rows[i]["ControlID"].ToString();
                if (sControlID == "tpInvoice")
                    tpInvoice.PageVisible = false;
            }

            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Visible = false;
                    txtCustID.BackColor = Color.Silver;
                    txtCustID.Properties.ReadOnly = true;
                    btnSubmit.Visible = false;
                    showInfo(_OrderID);
                    break;
                case Utility.OperateMode.Insert:
            
                    break;
                case Utility.OperateMode.Update:
                    btnSave.Enabled = true;
                    txtCustID.BackColor = Color.Silver;
                    txtCustID.Properties.ReadOnly = true;
                    showInfo(_OrderID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtCustSource, C_DMB.GetListByType(C_DMB.DType.CustSource.ToString()), "CodeID", "CodeName");

            Utility.DataToLookupComboxEx(txtPayType, C_DMB.GetListByType(C_DMB.DType.PayType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtPayStatus, C_DMB.GetListByType(C_DMB.DType.PayStatus.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtShippingType, C_DMB.GetListByType(C_DMB.DType.ShippingType.ToString()), "CodeID", "CodeName");

            txtOrderUserID.Enabled = false;
            txtOrderType.Enabled = false;
        }


        private void showInfo(string sID)
        {
            mOrder.GetModel(sID);
            this.txtOrderID.Text = mOrder.OrderID;
            this.txtCustID.Text = mOrder.POrderID;
            if (mOrder.OrderDate != null)
                this.txtOrderDate.DateTime = mOrder.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrder.OrderUserID;
            //this.txtOrderNo.Text = mOrder.OrderNo;
            //this.txtOrderCode.Text = mOrder.OrderCode;
            //this.txtSupplierID.EditValue = mOrder.SupplierID;
            this.txtCustID.Text = mOrder.CustID;
            this.txtOrderType.EditValue = mOrder.OrderType;
            //this.txtOrderStatus.EditValue = mOrder.OrderStatus;
            this.txtDepotID.EditValue = mOrder.DepotID;
            //this.txtToDepotID.EditValue = mOrder.ToDepotID;
            this.txtAuditUserID.EditValue = mOrder.AuditUserID;
            if (mOrder.AuditDate != null)
                this.txtAuditDate.DateTime = mOrder.AuditDate.Value;
            this.txtRemark.Text = mOrder.Remark;
            //this.txtConfirmUserID.Text = mOrder.ConfirmUserID;
            //this.txtConfirmDate.Text = mOrder.ConfirmDate.ToString();
            //this.txtSign.Text = mOrder.Sign.ToString();
            this.txtPayType.EditValue = mOrder.PayType;
            this.txtPayStatus.EditValue = mOrder.PayStatus;
            //this.txtCompositeNum.Text = mOrder.CompositeNum.ToString();
            //this.txtTransfer.Text = mOrder.Transfer;
            //this.txtSaleRemark.Text = mOrder.SaleRemark;
            this.txtShippingType.EditValue = mOrder.ShippingType;
            //this.txtRequiredDate.Text = mOrder.RequiredDate.ToString();
            this.txtShipingRequired.Text = mOrder.ShipingRequired;



            //*****************Order Detail*****************//
            BindOrderDetail();

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }



        private void BindOrderDetail()
        {
            BindSaleShipByOrderID(txtOrderID.Text);
        }

        private void BindSaleShipByOrderID(string sOrderID)
        {
            DataTable dtOrderDetail = T_Order.GetSaleShipByOrderID(sOrderID).Tables[0];
            gcSaleProduct.DataSource = dtOrderDetail;


            T_Order mOrder = new T_Order();
            DataSet dsmSaleProduct = mOrder.GetOrderList(" and a.POrderID='" + sOrderID + "'");
            gcOrderStock.DataSource = dsmSaleProduct.Tables[0];

            BindOrderInvoiceInfo(sOrderID);

        }


        private void BindOrderInvoiceInfo(string sOrderID)
        {
            T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
            DataTable dtOrderInvoiceInfoDetail = mOrderInvoice.GetSaleInvoiceInfoByOrderID(sOrderID).Tables[0];
            gcSaleInvoice.DataSource = dtOrderInvoiceInfoDetail;
        }

        private void txtCustID_EditValueChanged(object sender, EventArgs e)
        {
            string sCustID = txtCustID.Text;
            T_Customer mCustomer = new T_Customer();
            mCustomer.GetModel(sCustID);
            txtCustName.Text = mCustomer.CustName;
            txtCustSource.EditValue = mCustomer.CustSource;
            txtMobile.Text = mCustomer.Mobile;
            txtShipAddress.Text = mCustomer.ContactAddress;
        }

        private void txtCustID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmCustomer fDetail = new frmCustomer();
            fDetail.gFuncType = Utility.FuncType.Select;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCustID.Text = fDetail._CustID;
            }
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("此单据是否已完成？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            mOrder.OrderStatus = ((int)Utility.OrderStatus.S4PayAppAudited).ToString();
            //mOrder.ReceiptUserID = Utility.gUserID;
            //mOrder.ReceiptDate = DateTime.Now.Date;
            mOrder.Update();
            XtraMessageBox.Show("此销售单已完成回单!");
            DialogResult = System.Windows.Forms.DialogResult.OK;

        }

        private void btnSaleCancel_Click(object sender, EventArgs e)
        {
            frmSaleCancelDetail fSaleCancel = new frmSaleCancelDetail();
            fSaleCancel.OpMode = Utility.OperateMode.Insert;
            fSaleCancel._AuditStatus = Utility.OrderStatus.S0Input;
            fSaleCancel._OrderType = Utility.OrderType.SaleCancel;
            fSaleCancel._OrderID = txtOrderID.Text;
            fSaleCancel.ShowDialog();
        }

        private void btnSaleStock_Click(object sender, EventArgs e)
        {
            if (gvOrderStock.RowCount < 1) return;
            DataRow dr = gvOrderStock.GetDataRow(gvOrderStock.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmSaleShipStockDetail fDetail = new frmSaleShipStockDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.BuyToStock;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }

        private void btnReturnFee_Click(object sender, EventArgs e)
        {
            if (gvSaleInvoice.RowCount < 1) return;
            DataRow dr = gvSaleInvoice.GetDataRow(gvSaleInvoice.FocusedRowHandle);
            string sID = dr["InvoiceID"].ToString();

            frmSaleInvoiceReturnFee fDetail = new frmSaleInvoiceReturnFee();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._InvoiceID = sID;
            fDetail._OrderType = Utility.OrderType.SaleReturnFee;
            fDetail.ShowDialog();
        }





    }
}